#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include "ffmpeg.h"
#include <errno.h>
int main()
{
	output=malloc(FREENECT_DEPTH_11BIT_SIZE*30);
	myvideo=malloc(300000);
	int i;
	int outSize=123194;
	FILE *f;
  	/* open output file */
	if (!(f=fopen("out.mp4","r"))){
 		printf("Couldn't open output file\n");
    		exit(0);
  	}
	for(i=0;i<outSize;i++,myvideo++){
		//putc(*video,f);
		*myvideo=getc(f);
//printf("%c_%c",*video,(char)getc(f));
	}
  	fclose(f);
	myvideo=myvideo-outSize;

/*
//printf("%c_%d_%c_%d_%c_%d_%c_%d_ptr=%d\n",*(output),*(output),*(output+1),*(output+1),*(output+2),*(output+2),*(output+3),*(output+3),output);
	
	//printf("%c_%d_%c_%d_%c_%d_%c_%d_ptr=%d\n",*(output+3*FREENECT_VIDEO_RGB_SIZE),*(output+3*FREENECT_VIDEO_RGB_SIZE),*(output+1+3*FREENECT_VIDEO_RGB_SIZE),*(output+1+3*FREENECT_VIDEO_RGB_SIZE),*(output+2+3*FREENECT_VIDEO_RGB_SIZE),*(output+2+3*FREENECT_VIDEO_RGB_SIZE),*(output+3+3*FREENECT_VIDEO_RGB_SIZE),*(output+3+3*FREENECT_VIDEO_RGB_SIZE),output);
*/
videoSize=outSize;
printf("read video file size=%d\n",videoSize);
int argc=4;
char *argv[argc];
argv[0]="../ffmpeg-0.7-rc1/./ffmp";
argv[1]="-i";
argv[2]="try.mp4";
argv[3]="o%d.pgm";
int start=output;
printf("start output=%d\n",output);
isDepth=1;
myffmpeg(argc,argv);
printf("end output=%d\n",output-start);
output=output-FREENECT_DEPTH_11BIT_SIZE*30/2;




int rf;
int k=0;
char filename[20]="";
for(;k<30;k++){
snprintf(filename,20,"foo%d.pgm",k);
 if ((rf = open(filename,O_CREAT|O_TRUNC|O_WRONLY,0666))==-1)
  {
    printf("Couldn't create result.ppm\n");
		exit(0);
  }
char header[100]="P5\n640 480\n255\n";
//int j;
//for(j=0;j<1;j++){
  for (i=0; header[i]!=0; i++){
		write(rf,&header[i],1);
	}
int l=write(rf,output+FREENECT_DEPTH_11BIT_SIZE*k/2,FREENECT_DEPTH_11BIT_SIZE/2);

//}
//printf("written bytes=%s\n",(errno));
if(errno==EAGAIN)
	printf("1\n");
else if (errno==EBADF)
	printf("2\n");
else if (errno==EFAULT)
	printf("3\n");
else if (errno==EFBIG)
	printf("4\n");
else if (errno==EINTR)
	printf("5\n");
else if (errno==EINVAL)
	printf("6\n");
else if (errno==EIO)
	printf("7\n");
else if (errno==ENOSPC)
	printf("8\n");
else if (errno==EPIPE)
	printf("9\n");
else
	printf("10\n");
close(rf);
}

	return 0;
}

